package com.sp.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sp.shopping_common.exception.BusException;
import com.sp.shopping_common.pojo.ProductType;
import com.sp.shopping_common.result.CodeEnum;
import com.sp.shopping_common.service.ProductTypeService;
import com.sp.shopping_goods_service.mapper.ProductTypeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
@DubboService
@Transactional
public class ProductTypeImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public void add(ProductType productType) {
        //根据父类型计算当前类型是几级类型
        //查询父类型
        Long parentId = productType.getParentId();
        ProductType productType1 = productTypeMapper.selectById(parentId);
        /**
         * 如果没有父类型，则类型为1
         * 如果父类型级别<3该类型级别为父类型+1
         * 如果父类型级别>=3则不能添加该类型
         */
        if (productType1==null){
            productType.setLevel(1);
        }else if (productType1.getLevel()<3){
            productType.setLevel(productType1.getLevel()+1);
        } else if (productType1.getLevel()>=3) {
            throw new BusException(CodeEnum.INSERT_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.insert(productType);
    }

    @Override
    public void update(ProductType productType) {
        Long parentId = productType.getParentId();
        ProductType productType1 = productTypeMapper.selectById(parentId);
        /**
         * 如果没有父类型，则类型为1
         * 如果父类型级别<3该类型级别为父类型+1
         * 如果父类型级别>=3则不能添加该类型
         */
        if (productType1==null){
            productType.setLevel(1);
        }else if (productType1.getLevel()<3){
            productType.setLevel(productType1.getLevel()+1);
        } else if (productType1.getLevel()>=3) {
            throw new BusException(CodeEnum.INSERT_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.updateById(productType);
    }

    @Override
    public ProductType findById(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
         //如果该类型有子类型不允许删除
        //查询该类型的子类型
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parentid",id);
        List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
        //如果该类型有子类型删除失败
        if (productTypes !=null && productTypes.size()>0){
            throw new BusException(CodeEnum.DELETE_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.deleteById(id);
    }

    @Override
    public Page<ProductType> search(ProductType productType, int page, int size) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        if (productType !=null){
            //传来类型名
            if (StringUtils.hasText(productType.getName())){
                queryWrapper.like("name",productType.getName());
            }
            //传来上级类型id
            if (productType.getParentId() !=null){
                queryWrapper.eq("parentid",productType.getParentId());
            }
        }
        return productTypeMapper.selectPage(new Page(page,size),queryWrapper);
    }

    @Override
    public List<ProductType> findProductType(ProductType productType) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        if (productType !=null){
            //传来类型名
            if (StringUtils.hasText(productType.getName())){
                queryWrapper.like("name",productType.getName());
            }
            //传来上级类型id
            if (productType.getParentId() !=null){
                queryWrapper.eq("parentid",productType.getParentId());
            }
        }
        return productTypeMapper.selectList(queryWrapper);
    }
}
